﻿using System;
using System.Collections.Generic;

namespace NetTopologySuite.Triangulate.QuadEdge
{
	public class LastFoundQuadEdgeLocator : IQuadEdgeLocator
	{
		public LastFoundQuadEdgeLocator(QuadEdgeSubdivision subdiv)
		{
			this._subdiv = subdiv;
			this.Init();
		}

		private void Init()
		{
			this._lastEdge = this.FindEdge();
		}

		private QuadEdge FindEdge()
		{
			IEnumerator<QuadEdge> enumerator = this._subdiv.GetEdges().GetEnumerator();
			if (enumerator.MoveNext())
			{
				return enumerator.Current;
			}
			throw new IndexOutOfRangeException();
		}

		public QuadEdge Locate(Vertex v)
		{
			if (!this._lastEdge.IsLive)
			{
				this.Init();
			}
			QuadEdge quadEdge = this._subdiv.LocateFromEdge(v, this._lastEdge);
			this._lastEdge = quadEdge;
			return quadEdge;
		}

		private readonly QuadEdgeSubdivision _subdiv;

		private QuadEdge _lastEdge;
	}
}
